Vuex Quick Start Guide by Andrea Koutifaris
Author:Andrea Koutifaris
Language: eng
Format: epub
Tags: COM060080 - COMPUTERS / Web / General, COM060160 - COMPUTERS / Web / Web Programming, COM060180 - COMPUTERS / Web / Web Services and APIs
Publisher: Packt
Published: 2018-04-04T13:39:02+00:00
Using vue-loader for single-file components
Vue.js provides a webpack loader, vue-loader, to transform single-file components into JavaScript modules. To install vue-loader and related tools, type the following commands into a console:
npm install --save-dev vue-loader
npm install --save-dev vue-template-compiler
npm install --save-dev vue-style-loader
npm install --save-dev css-loader
npm install --save-dev file-loader
The file-loader is needed to import external files, such as images. The other packages are needed to tell webpack how to build all the pieces inside the .vue file.
Let's update webpack.config.js to use single-file components that will have a file extension of .vue:
// ...
const config = {
// ...
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.css$/,
use: [
'vue-style-loader',
'css-loader'
]
},
{
test: /\.(png|jpg|jpeg|gif|svg)$/,
loader: 'file-loader',
options: {
name: '[name].[ext]?[hash]'
}
}
]
},
resolve: {
alias: {
vue$: 'vue/dist/vue.esm.js',
},
},
// ...
};
// ...
Within the rules section inside the configuration, we tell webpack which loader to use when a file is imported inside a source file. In the preceding code, we configured webpack to use vue-loader for every .vue file, css-loader, and vue-style-loader for .css files, and file-loader for images.
In order to test that everything has been correctly configured, we will create an app.vue file:
// src/app.vue
<template>
<div class="app">App <span class="version">v{{version}}</span></div>
</template>
<script>
export default {
computed: {
version() {
return this.$store.state.version;
}
}
};
</script>
<style>
.app {
font-family: "Times New Roman", Times, serif;
background-image: url("./background.jpg");
}
</style>
You need a background.jpg file to build the preceding file. It is enough to put any image within the src folder and rename it as background.jpg.
This file uses Vuex.Store, the three parts of a Vue single-file component—<template>, <script>, and <style>—and an image as the background. This way, we are going to test vue-loader and its related packages, Vuex.Store, and file-loader for the background image.
Let's now update main.js to use app.vue:
// src/main.js
import Vue from 'vue';
import Vuex from 'vuex';
import app from './app.vue';
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(27123)
Hello! Python by Anthony Briggs(25975)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(25313)
Kotlin in Action by Dmitry Jemerov(24417)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(23613)
Dependency Injection in .NET by Mark Seemann(23332)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(21964)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(20870)
Grails in Action by Glen Smith Peter Ledbrook(19889)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(17084)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(16851)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(14479)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(12601)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(11877)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10657)
Hit Refresh by Satya Nadella(9259)
The Kubernetes Operator Framework Book by Michael Dame(8600)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8453)
Robo-Advisor with Python by Aki Ranin(8394)